<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="">
<head>
    <meta charset="utf-8">
    <title>收件人管理</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">收件人邮箱</label>
                    <div class="layui-input-inline">
                        <input id="email" type="text" name="email" placeholder="请输入收件人邮箱" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-md " lay-submit lay-filter="emailLog-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="emailLog-table" lay-filter="emailLog-table"></table>
    </div>
</div>

<script type="text/html" id="emailLog-toolbar">
    <button class="layui-btn layui-bg-blue layui-btn-md" lay-event="refresh">
        <i class="layui-icon layui-icon-refresh-1"></i>
        刷新
    </button>
    <button class="layui-btn layui-bg-red layui-btn-sm" lay-event="resend" id="resend" style="display: none">
        <i class="layui-icon layui-icon-release"></i>
        <span>重试</span>
    </button>
</script>

<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">

    layui.use(['table', 'form', 'jquery', 'upload', 'util'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let util = layui.util;
        window.id = parent.emailObjData.id
        $(document).ready(function () {
            if (parent.logStatus === 0) {
                $('#resend').show()
            }
        })
        let cols = [
            [{checkbox: true},
                {title: '收件人邮箱', field: 'email', align: 'left',},
                {title: '收件人姓名', field: 'name', align: 'left',},
                {title: '发送时间', field: 'sendTime', align: 'center'},
                {
                    title: '发送状态',
                    field: 'status',
                    sort: true,
                    align: "center",
                    templet: function (d) {
                        const statusMap = {
                            0: "失败",
                            1: "成功",
                        };
                        let statusText = statusMap[d.status] || "error";
                        return '<span>' + statusText + '</span>'
                    }
                },
                {
                    title: '失败消息', field: 'message', align: 'left',
                },
            ]
        ]

        table.render({
            elem: '#emailLog-table',
            url: baseURL + 'cms/email/getEmailLogByEmailId',
            request: {
                pageName: 'page',
                limitName: 'pageSize'
            },
            where: {
                emailId: parent.emailObjData.id,
                status: parent.logStatus
            },
            type: 'get',
            page: true,
            curr: 1,
            cols: cols,
            skin: 'line',
            toolbar: '#emailLog-toolbar',
            limits: [5, 10, 15, 20],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            response: {
                statusCode: 1000
            },
            done: function (data) {
                if (parent.logStatus === 1) {
                    table.hideCol('emailLog-table', {
                        field: 'message',
                        hide: true
                    });
                }
            },
        });
        table.on('toolbar(emailLog-table)', function (obj) {
            if (obj.event === 'refresh') {
                table.reload('emailLog-table', {
                    where: {emailId: parent.emailObjData.id, status: parent.logStatus},
                    page: {curr: 1}
                });
                if (parent.logStatus === 0) {
                    $('#resend').show()
                }
            } else if (obj.event === 'resend') {
                window.resend();
            }
        });

        form.on('submit(emailLog-query)', function (data) {
            table.reload('emailLog-table', {
                page: {
                    curr: 1
                },
                where: {
                    email: data.field.email,
                    emailId: parent.emailObjData.id
                }
            })
            return false;
        });

        //重新发送
        window.resend = function () {
            if (parent.emailObjData.status === 0 || parent.emailObjData.failNum === 0) {
                layer.msg("邮件是草稿或已全部发送成功", {icon: 0, time: 2000})
                return
            }
            if (parent.emailObjData.status === 2) {
                layer.msg("邮件正在发送中", {icon: 0, time: 2000})
                return
            }
            let remainingTries = 2 - parent.emailObjData.retry;
            if (remainingTries < 0) {
                layer.msg("重试次数已上限", {icon: 2, time: 1500})
                return
            }
            let confirmIndex = layer.confirm("是否需要修改邮件内容？", {
                btn: ['是', '否'] // 按钮
            }, function () {
                layer.open({
                    type: 2,
                    title: '编辑邮件内容',
                    shade: 0.1,
                    area: ['100%', '100%'],
                    content: 'content-edit'
                });
                layer.close(confirmIndex); // 关闭确认对话框
            }, function () {
                $.ajax({
                    url: baseURL + 'cms/email/resend',
                    data: {
                        id: parent.emailObjData.id,
                        sendTime: util.toDateString(Date.parse(new Date()), 'yyyy-MM-dd HH:mm:ss')
                    },
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 1000) {
                            layer.msg("发送成功，重试次数还剩" + remainingTries + "次", {
                                icon: 1,
                                time: 2000
                            })
                            table.reload('email-table', {where: {}, page: {curr: 1}});

                        } else {
                            layer.msg("发送失败，重试次数还剩" + remainingTries + "次", {
                                icon: 2,
                                time: 2000
                            });
                        }
                    },
                    error: function () {
                        layer.close(loading);
                        layer.msg("发送失败", {
                            icon: 2,
                            time: 2000
                        });
                    }
                })
                layer.close(confirmIndex); // 关闭确认对话框
            })
        }

    })
</script>
</body>
</html>
